package Grammar

object ParCollection {
  def main(args: Array[String]): Unit = {
    (1 to 5).foreach(println) //串行
    println("---------")
    (1 to 5).par.foreach(println) //并行
    println("######")

    val list = List(1, 2, 3, 4, "abc")
    list.filter(x => x.isInstanceOf[Int]).map(x => x.toString.toInt + 1).foreach(println)
    //偏函数处理多类型数据集合
    list.collect(new PartialFunction[Any, Int] {
      override def isDefinedAt(x: Any): Boolean = {
        if (x.isInstanceOf[Int]) true
        else false
      }

      override def apply(v1: Any): Int = {
        v1.asInstanceOf[Int] + 1
      }
    }).foreach(print)
    list.collect({ case i: Int => i + 1 })

    //闭包
    def makeSuffix(suffix: String) = {
      //返回一个匿名函数
      (name: String) => {
        if (name.endsWith(suffix)) name
        else name + suffix
      }
    }

    println()
    println(makeSuffix(".jpg")("test.jpg"))
  }


}
